/* 
 * Project: FlarDetector
 *
 * Copyright 2009, Longfellow Chen
 * --------------------------------------------------------------------------------
 * This work complements FLARToolkit, developed by Saqoosha as part of the Libspark project.
 *	http://www.libspark.org/wiki/saqoosha/FLARToolKit
 * FLARToolkit is Copyright (C)2008 Saqoosha,
 * and is ported from NYARToolkit, which is ported from ARToolkit.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this framework; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 * For further information please contact:
 *	<fellong0307@gmail.com>
 *	http://fellong.blogspot.com/
 * 
 */
 
 package com.fellong.flash.flar.util
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLLoader;
	import flash.net.URLLoaderDataFormat;
	import flash.net.URLRequest;
	import flash.utils.ByteArray;
	
	import org.libspark.flartoolkit.core.param.FLARParam;
	
	public class FlarParamLoader  extends EventDispatcher
	{
		private var _loader:URLLoader;
		private var _width:int;
		private var _height:int;
		
		public var _param:FLARParam;
		
		public function FlarParamLoader()
		{
		}
		
		public function init(cameraFile:String, canvasWidth:int = 320, canvasHeight:int = 240 ):void 
		{
			_width = canvasWidth;
			_height = canvasHeight;
			
			// setup ARToolkit
			_loader = new URLLoader();
			_loader.dataFormat = URLLoaderDataFormat.BINARY;
			_loader.addEventListener(Event.COMPLETE, onCOMPLETE);
			_loader.load(new URLRequest(cameraFile));
		}
		
		private function onCOMPLETE(e:Event):void {
			this._loader.removeEventListener(Event.COMPLETE, this.onCOMPLETE);
			this._param = new FLARParam();
			
			/*	output debug string
			var ba:ByteArray = _loader.data as ByteArray;
			var len:uint = ba.length;
			var s:String = "";
			for (var i:uint = 0; i < len; i++) {
 				if( ba[i]<0x10 )
 					s += "0"+ba[i].toString(16);
 				else
 					s += ba[i].toString(16);
 			}
			/*	input debug string
			var value:String = "00000280000001e04085e79c9c75d24700000000000000004073c80000000000000000000000000000000000000000004086b0c0e24f8e96406e3000000000000000000000000000000000000000000000000000000000003ff000000000000000000000000000004073e800000000004070780000000000403a3333333333333ff034402e71210c";
			var len:uint = value.length/2;
			var ba:ByteArray = new ByteArray();
			for (var i:uint = 0; i < len; i++) {
				var c:String = value.charAt(i*2) + value.charAt(i*2 + 1);
				ba.writeByte(parseInt(c, 16));
			}
			ba.position = 0;
			this._param.loadARParam(ba);
			*/
			
			this._param.loadARParam(_loader.data);
			this._param.changeScreenSize(_width, _height);
			
			this.dispatchEvent( new Event( Event.COMPLETE ) );
		}
		
	}
}